package com.madixin.chat.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.madixin.chat.mapper.UserMapper;
import com.madixin.chat.model.User;
import com.madixin.chat.security.LoginUserDetails;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;



@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    public UserDetailsService userDetailsService() {
        return new UserDetailsService() {
            @Override
            public UserDetails loadUserByUsername(String username) {
                // 使用 MyBatis-Plus 的 lambdaQuery 方法根据 phone 字段查询用户
                User user = lambdaQuery().eq(User::getPhone, username).one();
                if (user == null) {
                    throw new UsernameNotFoundException("用户不存在");
                }
                return new LoginUserDetails(user);
            }
        };
    }

}
